package com.yunyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunyi.common.core.domain.AjaxResult;
import com.yunyi.system.domain.EmrCaseSlide;
import com.yunyi.system.service.EmrCaseSlideService;
import com.yunyi.system.mapper.EmrCaseSlideMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【emr_case_slide(病例切片)】的数据库操作Service实现
* @createDate 2023-08-29 08:44:39
*/
@Service
public class EmrCaseSlideServiceImpl extends ServiceImpl<EmrCaseSlideMapper, EmrCaseSlide>
    implements EmrCaseSlideService{

    @Resource
    EmrCaseSlideMapper emrCaseSlideMapper;


    /**
     * 根据条件模糊查询并且分页
     * @param pageNum 当前页码
     * @param pageSize 每页条目
     * @param condition 查询条件
     * @return
     */
    @Override
    public AjaxResult listALlBycondition(Integer pageNum, Integer pageSize, Integer apmstatus, String condition) {
        PageHelper.startPage(pageSize,pageNum);
        List<EmrCaseSlide> emrCaseSlides = emrCaseSlideMapper.selectAllbycondition(apmstatus,condition);
        PageInfo<EmrCaseSlide> pageInfo=new PageInfo<>(emrCaseSlides);
        return emrCaseSlides==null?AjaxResult.error("预约冰冻查询失败,请检查,查询条件是否有误"):
                AjaxResult.success("预约冰冻查询成功",pageInfo);
    }

    /**
     * 修改预约状态
     * @param id
     * @return
     */
    @Override
    public AjaxResult updateapmstatusById(String id,String apmstatus) {
        int i = emrCaseSlideMapper.updateApmstatusById(id,apmstatus);
        return i>0?AjaxResult.success("预约成功"):AjaxResult.error("预约失败");
    }

    /**
     * 根据id删除预约
     * @param Id
     * @return
     */
    @Override
    public AjaxResult deleteById(String Id) {
        int i = emrCaseSlideMapper.deleteById(Id);
        return i>0?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }

    /**
     * 添加预约
     * @param emrCaseSlide
     * @return
     */
    @Override
    public AjaxResult insertCaseSide(EmrCaseSlide emrCaseSlide) {
        int insert = emrCaseSlideMapper.insert(emrCaseSlide);
        return insert>0?AjaxResult.success("添加成功"):AjaxResult.success("添加失败");
    }

    /**
     * 根据申请医院获取待分诊病例信息
     * @param hospital  申请医院
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public AjaxResult getCaseByHospital(Integer status,String hospital,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<EmrCaseSlide> caseByHospital = emrCaseSlideMapper.getCaseByHospital(status,hospital);
        PageInfo<EmrCaseSlide> casePageInfo = new PageInfo<>(caseByHospital);
        return caseByHospital == null ? AjaxResult.error("暂无待分诊数据") : AjaxResult.success("获取待分诊病例",casePageInfo);
    }

    @Override
    public AjaxResult getSection(String caseId) {
        List<EmrCaseSlide> section = emrCaseSlideMapper.getSection(caseId);
        return section == null ? AjaxResult.error("暂无切片信息") : AjaxResult.success(section);
    }

    @Override
    public AjaxResult getSlides(String caseNo) {
        QueryWrapper queryWrapper = new QueryWrapper<>().eq("case_id",caseNo);
        List<EmrCaseSlide> slides = emrCaseSlideMapper.selectList(queryWrapper);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Map<String, List<EmrCaseSlide>> slideMap = slides.stream()
                .collect(Collectors.groupingBy(slide -> formatDate(slide.getCreateDate(), formatter)));
        return slides == null ? AjaxResult.error("暂无切片") : AjaxResult.success("获取切片",slideMap);
    }

    @Override
    public AjaxResult allotCaseSlide(List<String> ids) {

        return emrCaseSlideMapper.allotCaseSlide(ids) > 0 ? AjaxResult.success("分配成功！") : AjaxResult.error("分配失败！");
    }

    @Override
    public AjaxResult revocation(String id, String reason) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>()
                .set("apmstatus",4)
                .set("reason",reason)
                .set("update_date",new Date())
                .eq("id",id);
        return emrCaseSlideMapper.update(null,updateWrapper) > 0 ? AjaxResult.success("撤回成功") : AjaxResult.error("撤回失败");
    }

    private String formatDate(Date date, DateTimeFormatter formatter) {
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime.format(formatter);
    }
}